package com.zhao.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhao.bean.Category;
import com.zhao.bean.PageParam;
import com.zhao.bean.R;
import com.zhao.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CategoryController {

    @Autowired
    private CategoryService cs;

    //添加
    @PostMapping("/Category")
    public R add(@RequestBody Category category) {
        int add = cs.add(category);
        R r = null;
        System.out.println("//////***********//////////" + add);
        if (add > 0) {
            r = R.success("添加成功");
        } else {
            r = R.error("添加失败");
        }
        return r;
    }

    //分页
    @GetMapping("/Category/{str}")
    public R find(@PathVariable String str) {
        PageParam pageParam = JSON.parseObject(str, PageParam.class);

        IPage<Category> page = cs.find(pageParam);

        return R.success(page);
    }

    //删除
    @DeleteMapping("/Category/{id}")
    public R delete(@PathVariable long id) {

        int delete = cs.delete(id);
        R r = null;
        if (delete > 0) {
            r = R.success("删除成功");
        } else {
            r = R.error("删除失败");
        }
        return r;
    }

    //修改
    @PutMapping("/Category")
    public R update(@RequestBody Category category) {

        int update = cs.update(category);
        if (update > 0) {
            return R.success("修改成功");
        } else {
            return R.error("修改失败");
        }

    }

    //查询菜品分类
    @GetMapping("/Category/locating/{type}")
    public R locating(@PathVariable int type) {
        try {
            List<Category> list = cs.locating(type);

            return R.success(list);

        } catch (Exception e) {
            e.printStackTrace();
            return R.error("查询失败");
        }

    }
}
